﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FrameWork.Lib;
using FrameWork.Lib.Serialization;
using FrameWork.Lib.DataObjects;

namespace AnimeWatchMax.DataObjects
{
    public class Data : DataObjectBase
    {
        public Data()
        {
        }

        public Data(bool pNew)
        {
            this.DataObjectGUID = Guid.NewGuid();
            this.Created = DateTime.Now;
            TitleList = new List<Title>();
            GroupList = new List<Group>();
            IgnoreTitleIDList = new List<string>();
        }

        public List<Title> TitleList { get; set; }
        public List<Group> GroupList { get; set; }
        public List<string> IgnoreTitleIDList { get; set; }
         
        public static Data Load(EventManager pEventManager)
        {
            pEventManager.AddResult(new Result("Loading Data....", ResultSeverityType.Info));

            Data tData = null;

            if (System.IO.File.Exists(Common.Constants.DataMainFilePath))
            {
                try
                {
                    var tXml = System.IO.File.ReadAllText(Common.Constants.DataMainFilePath);
                    tData = tXml.FromXML<Data>();
                }
                catch (Exception exc)
                {
                    pEventManager.AddResult(new Result(exc));

                    var tBackUpFileName = string.Format(Common.Constants.DataMainLoadBackUpFilePath, Guid.NewGuid());

                    System.IO.File.Move(Common.Constants.DataMainFilePath, tBackUpFileName);

                    pEventManager.AddResult(new Result(string.Format("Data Main file was backed up to {0}", tBackUpFileName), ResultSeverityType.Warning));
                }
            }

            if (tData == null)
            {
                pEventManager.AddResult(new Result("Initializing new data main for storing data.", ResultSeverityType.Info));

                tData = new Data(true);
            }
 

            pEventManager.AddResult(new Result("Loading Data Completed!", ResultSeverityType.Info));

            return tData;
        }

        public void Save(EventManager pEventManager)
        {
            var tXmlData = this.ToXml();

            try
            {
                pEventManager.AddResult(new Result("Saving Data....", ResultSeverityType.Info));
                System.IO.File.WriteAllText(Common.Constants.DataMainFilePath, tXmlData);
                pEventManager.AddResult(new Result("Saved Data!", ResultSeverityType.Info));
            }
            catch (Exception exc)
            {
                pEventManager.AddResult(new Result(exc));

                var tBackUpFileName = string.Format(Common.Constants.DataMainSaveBackUpFilePath, Guid.NewGuid());

                System.IO.File.WriteAllText(Common.Constants.DataMainSaveBackUpFilePath, tXmlData);

                pEventManager.AddResult(new Result(string.Format("Data Main Save file was backed up to {0}.", tBackUpFileName), ResultSeverityType.Warning));
            }


        }
    }
}
